var config = require('../../../utils/config');
var API_URL = config.API_URL;
var util = require('../../../utils/util');
var key,text, color;
var upload_P = util.wxPromise(my.uploadFile);
var patt = new RegExp("wxfile://");
var upload_img = (that) => {
    if (patt.test(that.data.cover)) {
        console.log('上传图片');
        return upload_P({
            url: API_URL.upload_img,
            filePath: that.data.cover,
            name: 'upload_file'
        })
    } else {
        console.log('网络图片');
        return new Promise((resolve, reject) => {
            var res = {};
            res.data = that.data.cover;
            resolve(res);
        })
    }
}
// 检查 检查成功加文本和样式 如果status不为1则继续显示检查
let check = (live, platform, that) => {
    util.POST(API_URL.live_check, { according_to: live, platform: platform })
        .then(res => {
            console.log(res)       
            let status = res.data.status;
            console.log(typeof(status))
            switch (status) {
                case 1: text = '直播间正在开播'; color = 'green'; break;
                case 0: text = '直播间未开播'; color = 'yellow'; break;
                case 200: text = '直播间无效'; color = 'red'; break;
                case 300: text = '直播间检测失败'; color = 'red'; break;
                default: break;
            }
            that.setData({
                status: status,
                text: text,
                color: color
            })
            console.log(that.data)
        }).catch(res => {
            console.log(res);
        })
}
Page({
    data: {
        code_disabled: false,
        code_time: '获取验证码',
        hiddenPlatform: false,
        platform: "",
        cover: "http://img.jadelibrary.com/image/b53d987c2b6ac1b635f2dbecfb3a0d39.jpg"
    },
    onLoad: function (options) {
        console.log(options)
        let that = this;
        let phone = options.phone;
        util.PRL(API_URL.get_live, {})
            .then(res => {
                console.log(res)
                if (res.data.data.length != 0) {
                    that.setData(res.data.data[0])
                }
            }).catch(res => {
                console.log(res);
            })
        util.PRL(API_URL.get_livePlatform, {})
            .then(res => {
                console.log(res)
                that.setData({
                    platforms: res.data.data,
                    phone: phone
                })
            }).catch(res => {
                console.log(res)
            })
    },
    // 选择直播平台
    choose: function (e) {
        console.log(e);
        let index = e.currentTarget.dataset.index;
        let platforms = this.data.platforms;
        console.log(platforms[index].platform)
        // 这里获取平台以便后迅判断
        this.setData({
            platform: platforms[index].platform,
            hiddenPlatform: false,
            platformCh: platforms[index].name
        })
    },
    // 显示直播平台
    change: function () {
        var hidden = !this.data.hiddenPlatform;
        this.setData({
            hiddenPlatform: hidden
        })
    },
    // 输入框检查直播间
    checkLive: function (e) {
        var that = this;
        let live = e.detail.value;
        let platform = this.data.platform;
        this.setData({
            link: live,
            status: -1
        })
        if (live != "") {
            check(live, platform, that);
        }
    },
    // 按钮重新检测
    recheck: function (e) {
        let live = this.data.link;
        let platform = this.data.platform;
        if (this.data.platformCh == undefined) {
            my.showToast({
                content: '请选择直播平台'
            });
            return
        }
        if (live != "") {
            check(live, platform, this);
        }
    },
    // 发送验证码
    send: function (e) {
        var url = API_URL.sendSms;
        var that = this;
        // 点击之后不能再次点击
        that.setData({
            code_disabled: true
        })
        console.log(that.data.phone)
        var params = {
            mobile: that.data.phone,
            type: 'bind_live'
        }
        util.PRL(url, params)
            .then(res => {
                console.log(res)
                if (res.data.status == 200) {
                    key = res.data.data.key;
                    util.countdown(that, 120);
                } else {
                    that.setData({
                        code_disabled: false
                    })
                    util.showNotice(res.data.msg)
                }
            }).catch(res => {
                that.setData({
                    code_disabled: false
                })
                console.log(res);
            })
    },
    // 修改封面
    edit: function (e) {
        let that = this;
        my.chooseImage({
            count: 1,
            sourceType: ['album', 'camera'],
            success: function (res) {
                that.setData({
                    cover: res.apFilePaths[0]
                })
            }
        })
    },
    // 绑定按钮
    bind: function (e) {
        let that = this;
        let code = e.detail.value.code;
        let live = e.detail.value.live;
        let platform = that.data.platform;
        let info = e.detail.value.info;
        if (platform == "") {
            util.showNotice('请选择直播平台')
            return
        }
        if (live == "") {
            util.showNotice('请输入直播号或直播间链接')
            return
        }
        if (code == '') {
            util.showNotice('请输入验证码')
            return
        }
        if (info == '') {
            util.showNotice('请输入直播间简介')
            return
        }
        upload_img(that)
            .then(res => {
                let params = {
                    live: live,
                    type: platform,
                    key: key,
                    code: code,
                    intro: info,
                    cover: res.data
                }
                return util.PRL(API_URL.bind_live, params)
            }).then(res => {
                if (res.data.status == 200) {
                    my.showToast({
                        content: '绑定成功',
                        duration: 800,
                    })
                    setTimeout(function () {
                        my.navigateBack({
                            delta: 1
                        })
                    }, 800)
                } else {
                    util.showNotice(res.data.msg)
                }
            }).catch(res => {
                console.log(res)
            })
    },
})